Page({
  data: {
    id: '',
    survey: null,
    isLoading: true,
    questions: [
      {
        id: 1,
        type: 'radio',
        title: '您的睡眠质量如何？',
        options: [
          { value: 'very_good', label: '非常好' },
          { value: 'good', label: '较好' },
          { value: 'normal', label: '一般' },
          { value: 'bad', label: '较差' },
          { value: 'very_bad', label: '非常差' }
        ],
        answer: ''
      },
      {
        id: 2,
        type: 'radio',
        title: '您平均每天的睡眠时间是多少？',
        options: [
          { value: 'less_6', label: '少于6小时' },
          { value: '6_7', label: '6-7小时' },
          { value: '7_8', label: '7-8小时' },
          { value: '8_9', label: '8-9小时' },
          { value: 'more_9', label: '超过9小时' }
        ],
        answer: ''
      }
    ]
  },

  onLoad(options) {
    const { id } = options;
    this.setData({ id });
    this.loadSurveyDetail();
  },

  async loadSurveyDetail() {
    this.setData({ isLoading: true });
    try {
      // TODO: 从服务器获取问卷详情
      // 这里使用模拟数据
      const surveyDetail = {
        id: this.data.id,
        title: this.data.id === '1' ? '睡眠质量评估问卷' : '睡眠习惯调查',
        description: this.data.id === '1' ? '评估您的睡眠质量状况' : '了解您的睡眠习惯',
        time: this.data.id === '1' ? 5 : 3
      };
      
      setTimeout(() => {
        this.setData({
          survey: surveyDetail,
          isLoading: false
        });
      }, 500);
    } catch (error) {
      console.error('加载问卷详情失败:', error);
      this.setData({ isLoading: false });
      wx.showToast({
        title: '加载失败',
        icon: 'error'
      });
    }
  },

  onAnswerChange(e) {
    const { questionId } = e.currentTarget.dataset;
    const { value } = e.detail;
    
    const questions = this.data.questions.map(q => {
      if (q.id === questionId) {
        return { ...q, answer: value };
      }
      return q;
    });
    
    this.setData({ questions });
  },

  onSubmit() {
    const unanswered = this.data.questions.some(q => !q.answer);
    if (unanswered) {
      wx.showToast({
        title: '请完成所有问题',
        icon: 'none'
      });
      return;
    }

    wx.showLoading({ title: '提交中...' });
    // TODO: 提交问卷答案到服务器
    setTimeout(() => {
      wx.hideLoading();
      wx.showToast({
        title: '提交成功',
        icon: 'success'
      });
      setTimeout(() => {
        wx.navigateBack();
      }, 1500);
    }, 1000);
  }
}) 